import axios from 'axios'

function empty() { }
const defaultOption = {
    success: empty,
    error: empty,
    data: {},
    url: ''
}

axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

export default function (option) {
    var option = Object.assign({}, defaultOption, option);

    axios({
        method: 'POST',
        url: option.url,
        headers: {
            'Content-Type': 'application/json',
        },
        data: option.data,
    }).then(function (response) {
        console.log(response);
        const { data } = response;
        if (response.status == 200) {
            if (data.status == -1) {
                option.error(data.msg || '请求失败');
                window.location.href="#/login";
            } else if (data.status == 1) {
                option.success(data.data);
            } else {
                option.error(data.msg || '请求失败');
            }
        } else {
            option.error(data.msg || '请求失败');
        }
    }).catch(function (error) {
        option.error('请求失败');
        console.log(error);
    });
}